package com.gofunbuy;

import com.gofunbuy.app.FirstActivity;
import com.gofunbuy.app.SecondActivity;
import com.gofunbuy.app.member.TabMemberActivity;
import com.gofunbuy.util.GoFunBuyUtils;

import android.app.ActivityGroup;
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

//第一个标签页显示的Activity，继承ActivityGroup，管理所有子Activity
public class TabMemberGroup extends ActivityGroup {
	private static final String TAG = "TabMemberGroup";
	// 用于管理本Group中的所有Activity
	public static ActivityGroup group;
	private static final int MSG_BACK_CLICKED = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		group = this;
	}

	@Override
	public void onBackPressed() {
		// 把后退事件交给子Activity处理
		// group.getLocalActivityManager().getCurrentActivity().onBackPressed();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_BACK) {
			exit();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	private boolean isExit = false;

	public void exit() {
		Log.v("tabMemberGroup", "exit");
		GoFunBuyUtils.mOrders.clear();
		if (!isExit) {
			isExit = true;
			Toast.makeText(getApplicationContext(), "再按一次退出程序",
					Toast.LENGTH_SHORT).show();
			mHandler.sendEmptyMessageDelayed(MSG_BACK_CLICKED, 2000);
		} else {
			Intent intent = new Intent(Intent.ACTION_MAIN);
			intent.addCategory(Intent.CATEGORY_HOME);
			startActivity(intent);
			System.exit(0);
		}
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_BACK_CLICKED:
				isExit = false;
				break;
			}
		}
	};

	@Override
	protected void onStart() {
		super.onStart();
		// 要跳转的Activity
		Intent intent = new Intent(this, TabMemberActivity.class);
		// 把Activity转换成一个Window，然后转换成View
		Window w = group.getLocalActivityManager().startActivity("member",
				intent);
		View view = w.getDecorView();
		// 设置要跳转的Activity显示为本ActivityGroup的内容
		group.setContentView(view);
	}

}
